package de.koller.million.card.frame.player;

import de.koller.million.card.CNode;
import de.koller.million.card.commands.RemoveCNode;
import de.koller.million.card.engine.player.Player;
import de.koller.aframe.util.thread.ICommand;
import java.text.DecimalFormat;

public class PlayerFrame extends CNode {

	public final Player player;
	private final PlayerFrameComp comp;

	private PlayerFrame( Player player ) {
		this.player = player;
		this.comp = new PlayerFrameComp( this );
	}
	
	@Override
	protected void init() {
		super.init();
		
		comp.setVisible( true );
		comp.setTitle( player.toString() );
		
		comp.pack();
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		

		comp.equipt.updateView( player.equipt );
		comp.detDamage.setText( "Schaden: " + Math.round( player.stats.getDamage() ) );
	}
	
	@Override
	protected void release() {
		super.release();
		
		player.view = null;
		comp.dispose();
	}

	public void deletePlayer() {
		this.croot.call( new ICommand() {
			@Override public void handle( Object... params ) {
				croot.engine.playerContainer.deletePlayer( player );
				closeThis();
			}
		} );
	}
	
	public void closeThis() {
		this.croot.call( new RemoveCNode( this ) );
	}
	
	public static void openPlayerView( Player player ) {
		if( player == null )
			return;
		
		player.croot.call( new OpenFrameCommand( player ) );
	}
	
	private static class OpenFrameCommand implements ICommand {

		private final Player player;

		public OpenFrameCommand( Player player ) {
			this.player = player;
		}
		
		@Override
		public void handle( Object... params ) {
			if( player.view != null ) {
				player.view.comp.requestFocus();
				return;
			}
			
			player.view = new PlayerFrame( player );
			player.croot.mainFrame.addChild( player.view );
		}
		
	}
	
}
